package br.com.pontoclass.primes.util;

import java.math.BigInteger;


public class MathLogicUtil {
    public static BigInteger convert(BigInteger value, BigInteger base) {
        StringBuilder newValue = new StringBuilder();
        while(value.divide(base).floatValue() > base.subtract(BigInteger.ONE).intValue()) {
            newValue.append(value.mod(base));
            value = value.divide(base);
        }
        newValue.append(value.mod(base));
        newValue.append(value.divide(base).intValue());
        return new BigInteger(newValue.reverse().toString());
    }
    
    public static BigInteger convertFrom(BigInteger base, BigInteger value) {
        String val = value.toString();
        int len, i = len = val.length();
        long result = 0;
        while(--i >= 0) {
            result += (Math.round(Integer.valueOf(Character.toString(val.charAt(len - (i+1)))) * Math.pow(base.longValue(), i)));
        }
        return new BigInteger(Long.toString(result));
    }
}
